#include "loginwidget.h"
#include"debug.h"
#include"model/datacenter.h"
#include"mainwidget.h"
#include<QMessageBox>

LoginWidget::LoginWidget(QWidget *parent)
    :QWidget(parent),isLoginMode(true)
{

    // 如果是在栈上定义的变量, 不能 delete, 会使程序崩溃.
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 1. 设置基本属性
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");

    // 2. 创建核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 0, 50, 0);
    layout->setSpacing(0);
    this->setLayout(layout);

    // 3. 创建标题
    tipLabel = new QLabel();
    tipLabel->setText("登录");
    tipLabel->setAlignment(Qt::AlignCenter);
    tipLabel->setFixedHeight(50);
    tipLabel->setStyleSheet("QLabel {font-size: 40px;}");

    // 4. 创建输⼊框
    usernameEdit = new QLineEdit();
    usernameEdit->setPlaceholderText("输⼊⽤⼾名");
    usernameEdit->setFixedHeight(40);
    usernameEdit->setStyleSheet("QLineEdit { padding-left: 10px; font-size:20px; border-radius: 5px; background-color: rgb(235, 235, 235); }");

    passwordEdit = new QLineEdit();
    passwordEdit->setPlaceholderText("输⼊密码");
    passwordEdit->setFixedHeight(40);
    passwordEdit->setStyleSheet("QLineEdit { padding-left: 10px; font-size:20px; border-radius: 5px; background-color: rgb(235, 235, 235); }");
    passwordEdit->setEchoMode(QLineEdit::Password);

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setPlaceholderText("输⼊验证码");
    verifyCodeEdit->setFixedHeight(40);

    verifyCodeEdit->setStyleSheet("QLineEdit { padding-left: 10px; font-size:20px; border-radius: 5px; background-color: rgb(235, 235, 235); }");
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    verifyCodeWidget = new VerifyCodeWidget();
    verifyCodeWidget->setFixedSize(100, 40);

    // 5. 创建提交按钮
    submitBtn = new QPushButton();
    submitBtn->setFixedHeight(40);
    submitBtn->setStyleSheet("QPushButton { color: rgb(255, 255, 255);background-color: rgb(44, 182, 61); border: none; border-radius: 5px;}\
                            QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    submitBtn->setText("登录");

    // 6. 创建切换⼿机号登录按钮
    phoneModeBtn = new QPushButton();
    phoneModeBtn->setFixedHeight(40);
    phoneModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    phoneModeBtn->setText("切换到⼿机号");

    // 7. 创建切换注册按钮
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedHeight(40);
    switchModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    switchModeBtn->setText("注册");

    // 8. 添加到布局管理器中
    layout->addWidget(tipLabel, 0, 0, 1, 5);
    layout->addWidget(usernameEdit, 1, 0, 1, 5);
    layout->addWidget(passwordEdit, 2, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 3, 0, 1, 4);
    layout->addWidget(verifyCodeWidget, 3, 4, 1, 1);
    layout->addWidget(submitBtn, 4, 0, 1, 5);
    layout->addWidget(phoneModeBtn, 5, 0, 1, 1);
    layout->addWidget(switchModeBtn, 5, 4, 1, 1);

    connect(submitBtn, &QPushButton::clicked, this, &LoginWidget::clickSubmitBtn);
    connect(switchModeBtn, &QPushButton::clicked, this, &LoginWidget::switchMode);
    connect(phoneModeBtn, &QPushButton::clicked, this, &LoginWidget::switchToPhone);
}

void LoginWidget::switchMode()
{
    if (isLoginMode) {
        // 切换到注册模式
        this->setWindowTitle("注册");
        tipLabel->setText("注册");
        submitBtn->setText("注册");
        switchModeBtn->setText("登录");
        isLoginMode = false;
    } else {
        // 切换到登录模式
        this->setWindowTitle("登录");
        tipLabel->setText("登录");
        submitBtn->setText("登录");
        switchModeBtn->setText("注册");
        isLoginMode = true;
    }
}

void LoginWidget::switchToPhone()
{
    PhoneLoginWidget* widget = new PhoneLoginWidget();
    widget->show();
    // 关闭当前窗⼝
    this->close();
}

void LoginWidget::clickSubmitBtn()
{
    // 1. 获取到必要的输⼊框内容
    const QString& username = usernameEdit->text();
    const QString& password = passwordEdit->text();
    const QString& verifyCode = verifyCodeEdit->text();

    if (username.isEmpty() || password.isEmpty() || verifyCode.isEmpty()) {
        // QMessageBox::warning(this, "提⽰", "⽤⼾名/密码不能为空!");
        LOG() << "⽤⼾名/密码/验证码不能为空!";
        return;
    }

    // 2. 验证验证码是否匹配
    if (!verifyCodeWidget->checkVerifyCode(verifyCode)) {
        // QMessageBox::warning(this, "提⽰", "验证码不正确!");

        LOG() << "验证码不正确!";

        //同时刷新验证码
        verifyCodeWidget->refreshVerifyCode();

        return;
    }

    // 3. 真正发起⽹络请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if (isLoginMode) {
        connect(dataCenter, &model::DataCenter::userLoginDone, this, &LoginWidget::userLoginDone, Qt::UniqueConnection);
        dataCenter->userLoginAsync(username, password);
    } else {
        connect(dataCenter, &model::DataCenter::userRegisterDone, this, &LoginWidget::userRegisterDone, Qt::UniqueConnection);
        dataCenter->userRegisterAsync(username, password);
    }
}

void LoginWidget::userLoginDone(bool ok, const QString &reason)
{
    if (!ok) {
        LOG() << "登录失败，错误原因：" << reason;
        return;
    }

    // 1. 登录成功, 跳转到主界⾯
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();
    // 2. 关闭当前窗⼝
    this->close();
}

void LoginWidget::userRegisterDone(bool ok, const QString &reason)
{
    if (!ok) {
        LOG() << "注册失败，错误原因：" << reason;
        return;
    }

    QMessageBox::warning(this, "提⽰", "注册成功!");
    //Toast::showMessage("注册成功!");

    // 跳转到登录界⾯
    switchMode();
    // 清空输⼊框
    usernameEdit->setText("");
    passwordEdit->setText("");
    verifyCodeEdit->setText("");
    // 更新验证码
    verifyCodeWidget->refreshVerifyCode();
}


#include "phoneloginwidget.h"

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
    : QWidget(parent), isLoginMode(true)
{
    // 1. 设置基本属性
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    // 如果是在栈上定义的变量, 不能 delete, 会使程序崩溃.
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 0, 50, 0);
    layout->setSpacing(0);
    this->setLayout(layout);

    // 3. 创建标题
    tipLabel = new QLabel();
    tipLabel->setText("登录");
    tipLabel->setAlignment(Qt::AlignCenter);
    tipLabel->setFixedHeight(50);
    tipLabel->setStyleSheet("QLabel {font-size: 40px;}");

    // 4. 创建输⼊框
    phoneEdit = new QLineEdit();
    phoneEdit->setPlaceholderText("输⼊电话");
    phoneEdit->setFixedHeight(40);
    phoneEdit->setStyleSheet("QLineEdit { padding-left: 10px; font-size: 20px;border-radius: 5px; background-color: rgb(235, 235, 235); }");

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setPlaceholderText("输⼊验证码");
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setStyleSheet("QLineEdit { padding-left: 10px; font-size:20px; border-radius: 5px; background-color: rgb(235, 235, 235); }");
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    verifyCodeBtn = new QPushButton();
    verifyCodeBtn->setFixedSize(100, 40);
    verifyCodeBtn->setStyleSheet("QPushButton {border: none;}");
    verifyCodeBtn->setText("发送验证码");

    // 5. 创建提交按钮
    submitBtn = new QPushButton();
    submitBtn->setFixedHeight(40);
    submitBtn->setStyleSheet("QPushButton { color: rgb(255, 255, 255);background-color: rgb(44, 182, 61); border: none; border-radius: 5px;}\
                            QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    submitBtn->setText("登录");

    // 6. 创建切换⼿机号登录按钮
    switchToUsernameBtn = new QPushButton();
    switchToUsernameBtn->setFixedHeight(40);
    switchToUsernameBtn->setStyleSheet("QPushButton { border: none; borderradius: 5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    switchToUsernameBtn->setText("切换到⽤⼾名"); 

    // 7. 创建切换注册按钮
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedHeight(40);
    switchModeBtn->setStyleSheet("QPushButton { border: none; border-radius:5px;} QPushButton:pressed {background-color: rgb(235, 235, 235);}");
    switchModeBtn->setText("注册");

    // 8. 添加到布局管理器中
    layout->addWidget(tipLabel, 0, 0, 1, 5);
    layout->addWidget(phoneEdit, 1, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 3, 0, 1, 4);
    layout->addWidget(verifyCodeBtn, 3, 4, 1, 1);
    layout->addWidget(submitBtn, 4, 0, 1, 5);
    layout->addWidget(switchToUsernameBtn, 5, 0, 1, 1);
    layout->addWidget(switchModeBtn, 5, 4, 1, 1);

    connect(switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);
    connect(switchToUsernameBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchToUsername);
    connect(verifyCodeBtn, &QPushButton::clicked, this,&PhoneLoginWidget::sendVerifyCode);
    connect(submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);

    //9. 初始化 QTimer
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=]() {
        if (leftTime <= 1) {
            // 计时结束.
            verifyCodeBtn->setEnabled(true);
            verifyCodeBtn->setText("发送验证码");
            timer->stop();
            return;
        }
        leftTime--;
        verifyCodeBtn->setText(QString::number(leftTime) + "s");
    });
}

void PhoneLoginWidget::sendVerifyCode()
{
    // 1. 获取到输⼊框内容
    const QString& phone = phoneEdit->text();
    if (phone.isEmpty()) {
        LOG() << "电话号码为空!";
        return;
    }

    // 2. 开始定时器
    // 启动定时器, 在⼀定时间之后恢复按钮可⽤.
    leftTime = 30;
    timer->start(1000);

    // 3. 给服务器发请求, 获取验证码
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getVerifyCodeDone, this, &PhoneLoginWidget::sendVerifyCodeDone, Qt::UniqueConnection);
    dataCenter->getVerifyCodeAsync(phone);
}

void PhoneLoginWidget::sendVerifyCodeDone()
{
    LOG() << "验证码请求已发送!";
}

void PhoneLoginWidget::clickSubmitBtn()
{
    // 1. 获取到必要的内容
    const QString& phone = phoneEdit->text();
    const QString& verifyCode = verifyCodeEdit->text();
    if (phone.isEmpty() || verifyCode.isEmpty()) {
        LOG() << "电话号码/短信验证码不能为空!";
        return;
    }

    // 更新定时器, 恢复发送按钮的状态
    leftTime = 1;

    // 2. 发送请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if (isLoginMode) {
        connect(dataCenter, &model::DataCenter::phoneLoginDone, this, &PhoneLoginWidget::phoneLoginDone, Qt::UniqueConnection);
        dataCenter->phoneLoginAsync(phone, verifyCode);
    } else {
        connect(dataCenter, &model::DataCenter::phoneRegisterDone, this, &PhoneLoginWidget::phoneRegisterDone, Qt::UniqueConnection);
        dataCenter->phoneRegisterAsync(phone, verifyCode);
    }
}

void PhoneLoginWidget::phoneLoginDone(bool ok, const QString& reason)
{
    if (!ok) {
        LOG() << "登录失败，错误原因：" << reason;
        return;
    }

    // 1. 登录成功, 跳转到主界⾯
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();
    // 2. 关闭当前窗⼝
    this->close();
}

void PhoneLoginWidget::phoneRegisterDone(bool ok, const QString &reason)
{
    if (!ok) {
        LOG() << "注册失败! " + reason;
        return;
    }
    // 提⽰注册成功
    LOG() << "注册成功!";
    // 跳转到登录界⾯
    switchMode();
    // 清空输⼊框
    phoneEdit->setText("");
    verifyCodeEdit->setText("");
    verifyCodeBtn->setText("发送验证码");
    verifyCodeBtn->setEnabled(true);
}

void PhoneLoginWidget::switchMode()
{
    //将定时器关闭
    leftTime = 1;
    if (isLoginMode) {
        // 切换到注册模式
        this->setWindowTitle("注册");
        tipLabel->setText("注册");
        submitBtn->setText("注册");
        switchModeBtn->setText("登录");
        isLoginMode = false;
    } else {
        // 切换到登录模式
        this->setWindowTitle("登录");
        tipLabel->setText("登录");
        submitBtn->setText("登录");
        switchModeBtn->setText("注册");
        isLoginMode = true;
    }
}

void PhoneLoginWidget::switchToUsername()
{
    LoginWidget* widget = new LoginWidget();
    widget->show();
    this->close();
}
